package TUILayer;
import ModelLayer.Order;
import CTRLayer.OrderCtr;

/**
 * A class to manage the interaction between the user and the order controller
 * 
 * @author Mike M. Madsen, Rasmus A. Borup
 * @version 15/12-2013
 */
public class OrderTUI
{
    // Instance variables
    private String searchEAN;
    private String orderID = null;
    private OrderCtr orderCtr;
    
    /**
     * Constructor for objects of class OrderTUI
     */
    public OrderTUI()
    {
        orderCtr = new OrderCtr();
    }

    
    /**
     * Creates a new order object and adds a customer. If a customer
     * isn't found prints an error message and returns to the order menu
     */
    public String newOrder(String customerType, String customerID, boolean wantedDelivery)
    {
        if(orderCtr.getCorporateCustomerCtr().getCorporateCustomer(customerID) != null)
        {
            orderID = orderCtr.addOrder(customerType, customerID, wantedDelivery);
        }
        else
        {
        	orderID = null;
        }
        return orderID;
    }
    
    /**
     * Returns an order, wich will be found by searching for it's ID.
     */
    
    public Order searchOrder(String searchID)
    {
    	return orderCtr.getOrder(searchID);
    }
    
    /**
     * Creates and adds a partorder to the last created order. If the 
     * product for the partorder isn't found prints an error message and 
     * returns to the order manager.
     */
    public double addPartOrder(int amount, String EAN, String orderID)
    {
    	double price = 0;
        if(orderCtr.getproductCtr().findProductByEAN(EAN) != null)
        {
            price = orderCtr.createPartOrder(amount, EAN, orderID);
        }
        return price;
    }
    
    public void removePartOrder(int i, String orderId)
    {
    	orderCtr.getOrder(orderId).removePartOrder(i);
    }
    
    /**
     * Removes an order from the system determined by orderID. If the order
     * isn't found prints an error message and returns to the order menu
     */
    private void removeOrder(String orderID)
    {
        if(orderCtr.getOrder(orderID) != null)
        {
            orderCtr.removeOrder(orderID);
        }
    }
    
    public Order getOrder(String orderID)
    {
    	return orderCtr.getOrder(orderID);
    }
}
